package com.itheima.tlias84.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias84.Po.InsertStudentParam;
import com.itheima.tlias84.Po.SelectStudentPageParam;
import com.itheima.tlias84.Po.UpdateStudentViolationParam;
import com.itheima.tlias84.Service.StudentService;
import com.itheima.tlias84.Util.ResultUtil;
import com.itheima.tlias84.annotation.Log;
import com.itheima.tlias84.controller.SelectStudentCountDTO;
import com.itheima.tlias84.dto.SelectStudentDTO;
import com.itheima.tlias84.dto.SelectStudentDegreeDataDTO;
import com.itheima.tlias84.entity.Student;
import com.itheima.tlias84.mapper.StudentMapper;
import com.itheima.tlias84.vo.PageVO;
import com.itheima.tlias84.vo.SelectStudentCountDataVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class StudentServiceImpl implements StudentService {
 @Resource
 StudentMapper studentMapper;



    @Override
    public SelectStudentDTO selectStudentById(Integer id) {
        return studentMapper.selectStudentById(id);
    }

    @Override
    public ResultUtil selectStudentByPage(SelectStudentPageParam param) {
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectStudentDTO> dtos = studentMapper.selectStudentByPage(param);
        PageVO<SelectStudentDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(dtos);
        return ResultUtil.success(pageVO);
    }

    @Log
    @Override
    public int insertStudent(InsertStudentParam param) {
        Student build = Student.builder()
                .name(param.getName())
                .no(param.getNo())
                .gender(param.getGender())
                .phone(param.getPhone())
                .idCard(param.getIdCard())
                .isCollege(param.getIsCollege())
                .address(param.getAddress())
                .degree(param.getDegree())
                .graduationDate(param.getGraduationDate())
                .clazzId(param.getClazzId())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        return studentMapper.insertStudent(build);

    }

    @Log
    @Override
    public int deleteById(Integer id) {
        return studentMapper.deleteById(id);
    }

    @Log
    @Override
    public ResultUtil updateStudentById(Student student) {
     student.setUpdateTime(LocalDateTime.now());
        SelectStudentDTO dto = studentMapper.selectStudentByName(student.getName());
        if(Objects.nonNull(dto)&&dto.getId()!=student.getId()){
            return ResultUtil.fail("该学生已存在");
        }
        studentMapper.updateStudentById(student);
        return ResultUtil.success("修改学生信息成功");
    }

    @Log
    @Override
    public int updateStudentViolation(Integer id, Integer score) {
        UpdateStudentViolationParam ViolationParam = new UpdateStudentViolationParam();
        SelectStudentDTO selectStudentDTO = studentMapper.selectStudentById(id);
        ViolationParam.setId(id);
        ViolationParam.setViolationScore(selectStudentDTO.getViolationScore()+score);
        ViolationParam.setViolationCount(selectStudentDTO.getViolationCount()+1);
        ViolationParam.setUpdateTime(LocalDateTime.now());
        return studentMapper.updateStudentViolation(ViolationParam);

    }

    @Override
    public List<SelectStudentDegreeDataDTO> selectStudentDegreeData() {
        return studentMapper.selectStudentDegreeData();
    }

    @Override
    public ResultUtil selectStudentCount() {
        List<SelectStudentCountDTO> dtos = studentMapper.selectStudentCount();
        SelectStudentCountDataVO<String> StudentCountVO = new SelectStudentCountDataVO<>();
        List<String> clazzList = dtos.stream().map(t -> t.getName()).toList();
        List<Integer> dataList = dtos.stream().map(t -> t.getValue()).toList();
        StudentCountVO.setClazzList(clazzList);
        StudentCountVO.setDataList(dataList);
        return ResultUtil.success(StudentCountVO);

    }




}
